package com.whr.controller;

import com.whr.bean.Columns;
import com.whr.bean.ResMenu;
import com.whr.bean.Resources;
import com.whr.bean.User;
import com.whr.service.ProbService;
import com.whr.service.ResService;
import com.whr.service.RoleService;
import com.whr.service.UserService;
import com.whr.utils.CompareResources;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private ResService resService;
    @Resource
    private ProbService probService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session, RedirectAttributes attributes) {
        String tip = "";
        User user0 = userService.login(user);
        if (user0 == null) {
            tip = "用户名或密码错误";
            attributes.addFlashAttribute("tip", tip);
            return "redirect:/user/index/welcome";
        }
        if (user0 != null) {
            if (user0.getState().equals("禁用")) {
                tip = "此用户已被禁用";
                attributes.addFlashAttribute("tip", tip);
                return "redirect:/user/index/welcome";
            }
        }
        tip = "success";
        attributes.addFlashAttribute("tip", tip);
        session.setAttribute("user", user0);
        return "redirect:/user/index/welcome";
    }

    @RequestMapping("/main")
    public String main(HttpSession session, HttpServletResponse response, Model model) throws IOException {
        User user = (User) session.getAttribute("user");
        if (user == null){
            return "redirect:/user/index/welcome";
        }
        String role = user.getRole();
        String path = roleService.getPathByRole(role);
        List<ResMenu> resMenus = new ArrayList<>();
        List<Resources> reses0 = resService.findMenuByPath(path);
        List<Resources> reses = new CompareResources().sortRes(reses0);
        for (Resources res : reses) {
            if (res.getRparent().equals("") || res.getRparent() == null) {
                ResMenu resMenu = new ResMenu();
                String ricon = res.getRicon();
                resMenu.setParent(res.getRname());
                resMenu.setIcon(ricon);
                List<Resources> reses1 = new ArrayList<>();
                for (Resources res1 : reses) {
                    if (res1.getRparent().equals(res.getRname())) {
                        res1.setRname(res1.getRname().replaceAll("……", ""));
                        resMenu.getChild().add(res1);
                    }
                }
                resMenus.add(resMenu);
            }
        }
        model.addAttribute("resMenus", resMenus);
        return "main";
    }

    @RequestMapping("/exit")
    public String exit(HttpSession session) {
        session.invalidate();
        return "index";
    }

    @RequestMapping("/error")
    public String error(){
        return "error";
    }

    @RequestMapping("/modify")
    @ResponseBody
    public Integer modify(String spass, String npass, Integer uid) {
        User user = userService.findById(uid);
        if (!spass.equals(user.getPassword())) {
            return 0;
        }
        userService.upPass(npass, uid);
        return 1;
    }

    @RequestMapping("/index/{path}")
    public String index(@PathVariable("path") String path, Model model) {
        List<Columns> columns = resService.findColAll();
        List<String> probs = probService.findProb7();
        List<String> fines = probService.findFine7();
        List<String> replys = probService.findReply5();
        String prob7 = userService.genProb(probs);
        String fine7 = userService.genFine(fines);
        String reply5 = userService.genReply(replys);
        String rank = userService.genRank();
        String count = userService.genCount();

        for (Columns col : columns) {
            if (col.getCflag().equals("近期问题")) {
                col.setContent(prob7);
            }
            if (col.getCflag().equals("反馈精选")) {
                col.setContent(fine7);
            }
            if (col.getCflag().equals("最新回复")) {
                col.setContent(reply5);
            }
            if (col.getCflag().equals("排行榜")) {
                col.setContent(rank);
            }
            if (col.getCflag().equals("数据统计")) {
                col.setContent(count);
            }
        }
        model.addAttribute("columns", columns);
        if (path.equals("home")) {
            return "home";
        }
        return "welcome";
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> users = userService.findAll();
        List<String> rolename = roleService.selRoleNick();
        model.addAttribute("users", users);
        model.addAttribute("rolename", rolename);
        return "userList";
    }

    @RequestMapping("/add")
    public String add(User user) {
        userService.saveUser(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/toadd")
    public String toadd(Model model) {
        List<String> rolename = roleService.selRoleNick();
        model.addAttribute("rolename", rolename);
        return "/userAdd";
    }

    @RequestMapping("/stop")
    public String stop(Integer[] checks) {
        userService.stop(checks);
        return "redirect:/user/list";
    }

    @RequestMapping("/start")
    public String start(Integer[] checks) {
        userService.start(checks);
        return "redirect:/user/list";
    }

    @RequestMapping("/toedit")
    public String toedit(Integer checks, Model model) {
        User user = userService.findById(checks);
        model.addAttribute("user", user);
        return "userEdit";
    }

    @RequestMapping("/edit")
    public String edit(User user) {
        userService.updateUser(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/del")
    public String del(Integer[] checks) {
        userService.delUser(checks);
        return "redirect:/user/list";
    }

    @RequestMapping("/rese")
    public String rese(Integer[] checks) {
        userService.rese(checks);
        return "redirect:/user/list";
    }

    @RequestMapping("/batch/{parm}")
    public String batch(@PathVariable("parm") String parm) {
        userService.delParm(parm);
        return "redirect:/user/list";
    }

    @RequestMapping("/editRole/{role}")
    public String editRole(@PathVariable("role") String role, Integer[] checks) {
        for (Integer uid : checks) {
            userService.updateRole(role, uid);
        }
        return "redirect:/user/list";
    }
}
